package com.landary.parameterrplacement.service;

import com.landary.parameterrplacement.entity.User;
import com.landary.parameterrplacement.utils.JDBCUtils;
import com.landary.parameterrplacement.utils.XmlToMapUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @author 刘欣
 * @createTime 2023/7/31 9:18
 * @description 获取用户
 */
@Service
@Slf4j
public class AnalysisUserXmlService {

    @Autowired
    private JDBCUtils jdbcUtils;

    /**
     * xml转json
     *
     * @param xml
     * @param tagName
     * @return
     * @throws Exception
     */
    public List<User> userXmlToMap(String xml, String tagName) throws Exception {
        List<Map<String, String>> listMap = (List<Map<String, String>>) XmlToMapUtils.xmlToMap(xml, tagName);
        List<User> users = User.mapToUserList(listMap);
        //查询mk组织架构数据库，获取所有人员
       List<User> dbUsers = getDBUsers();
//        List<User> dbUsers = getDBUserTest();
//        log.info("数据库组织人员数据" + dbUsers);
        List<User> changeUsers = dbUsers.stream()
                .filter(dbUserItem -> users.stream()
                        .noneMatch(userItem -> userItem.getAvicUserAccount().equals(dbUserItem.getAvicUserAccount())))
                .collect(Collectors.toList());
        if (changeUsers.size() > 0 || changeUsers != null) {
            for (User changeUser : changeUsers) {
                User user = new User();
                user.setAvicUserAccount(changeUser.getAvicUserAccount());
                user.setFullName(changeUser.getFullName());
                user.setMail(changeUser.getMail());
                user.setIsAvailable(false);
                users.add(user);
            }
        }
        log.info("无效人员" + changeUsers);
        if (users == null || users.size() == 0) {
            throw new Exception("xml解析有误！");
        }
        return users;
    }

    /**
     * 获取mk组织架构人员
     *
     * @return
     */
    public List<User> getDBUsers() {
        log.info("查询数据库中组织人员");
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        List<User> users = new ArrayList<>();
        log.info("建立数据连接");
        try {
            conn = jdbcUtils.getConnection();
            stmt = conn.createStatement();
            String sql = "SELECT FD_EMAIL,FD_NAME,FD_ORDER,FD_ORI_ID FROM SYS_ORG_ELEMENT  WHERE FD_ORG_TYPE = 8";
            rs = stmt.executeQuery(sql);
            log.info("执行的sql", sql);
//            log.info("SQL执行结果集", rs.toString());
            while (rs.next()) {
                String fdid = rs.getString("FD_ORI_ID");
//                log.info("获取结果集中的fdId" + fdid);
                if (fdid != null && fdid.contains("person:")) {
                    User user = new User();
                    String res = fdid.replace("person:", "");
                    user.setAvicUserAccount(res);
                    user.setMail(rs.getString("FD_EMAIL"));
                    user.setFullName(rs.getString("FD_NAME"));
                    user.setAvicSortOrder(Integer.parseInt(rs.getString("FD_ORDER")));
                    users.add(user);
                }
            }
        } catch (SQLException | ClassNotFoundException e) {
            e.printStackTrace();
            log.info("SQL语句执行异常", e);
        } finally {
            JDBCUtils.close(conn, stmt, rs);
        }
        log.info("查询结束");
        return users;
    }

    /**
     * 测试数据
     *
     * @return
     */
    public List<User> getDBUserTest() {
        List<User> users = new ArrayList<>();
        User user = new User();
        user.setAvicUserAccount("zhangsan");
        users.add(user);
        User user1 = new User();
        user1.setAvicUserAccount("lisi");
        users.add(user1);
        User user2 = new User();
        user2.setAvicUserAccount("wailairenyuan");
        users.add(user2);
        return users;
    }
}
